﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全角转半角</title>
    <script type="text/javascript">
        function fullChar2halfChar(str){
            var result ='';
            for(i = 0;i< str.length;i++){
                code = str.charCodeAt(i);
                if (code >= 65281 && code <= 65373)   //unicode编码范围是所有的英文字母以及各种字符
                {
                    result += String.fromCharCode(str.charCodeAt(i) - 65248);    //把全角字符的unicode编码转换为对应半角字符的unicode码
                }
                else if (code == 12288)                                      //空格
                {
                    result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);//半角空格
                }else
                {
                    result += str.charAt(i);                                     //原字符返回
                }
            }
            return result;
        }
    </script>
</head>
<body>
全角<input type=text name="txt1" value="ｔｈｉｓ　ｉｓ　！"><br />
半角<input type=text name="txt2" value="">
<input type=button value="转换文本" onClick="txt2.value=fullChar2halfChar(txt1.value)">
</body>
</html>